package com.geek.javase.ch16.work.work1;

/*
 *@author wangpeng
 *2025/11/2
 *@description TODO
 */

import java.util.concurrent.CountDownLatch;

/**
 *  * 需求：
 *  * 线程1要执行打印：A和C，线程2要执行打印：B，
 *  * 但线程1在打印A后，要线程2打印B之后才能打印C，
 *  * 所以：线程1在打印A后，必须等待线程2打印完B之后才能继续执行。
 *  *
 *  * 多线程的情况下 C -> B -> A
 *  * sleep()、join()、CountDownLatch类、CyclicBarrier类、阻塞队列LinkedBlockingQueue，PriorityBlockingQueue等等
 */
public class CountDownLatchTest {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        new Thread(() -> {
            System.out.println("A");
            try {
                countDownLatch.countDown();
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("C");
        }).start();
        new Thread(() -> {
            System.out.println("B");
            countDownLatch.countDown();
        }).start();
    }
}
